/*
 * @Author: sjf 1758142861@qq.com
 * @Date: 2023-03-21 19:02:16
 * @LastEditors: sjf 1758142861@qq.com
 * @LastEditTime: 2023-03-21 19:15:59
 * @FilePath: /D_C_lib/lw_process/day4/mydetach.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<pthread.h>
#include<unistd.h>
#include<sys/types.h>

const int NUM = 5;

void* _start_routine(void* args)
{

    pthread_detach(pthread_self());
    const char* name = static_cast<const char*>(args);
    int count = 5;
    while(count--)
    {
        std::cout << "我是一个新进程,my name:" << args << "tid:" << pthread_self() << std::endl;
        sleep(1);
    }
}

int main()
{
    pthread_t tid[NUM];
    for(int i = 0; i < 6; i++)
    {
        char namebuffer[64];
        snprintf(namebuffer,sizeof(namebuffer),"thread:%d",i);
        pthread_create(&tid[i],nullptr,_start_routine,(void*)namebuffer);
    }
    while(true)
    {
        std::cout << "我是主线程,pid:" << getpid() << "tid:" << pthread_self() << std::endl;
        sleep(1);
    }
    return 0;
}